<?php

namespace App\Http\Controllers;

use App\Models\AdminModels;
use Illuminate\Http\Request;


class AdminController extends Controller
{
    // 查询管理员
    public function index(Request $request){
        $username = $request->input('username');
        $start = $request->input('start');
        $end = $request->input('end');
        // 查询所有管理员
        $model = new AdminModels();
        // 闭包
        $data = $model->where(function ($query) use ($username, $start, $end){
            // 搜索
            if (!empty($username)){
                $query->where('username', 'like', '%'.$username.'%');
            }
            // 查询开始时间，没有结束时间
            if (!empty($start && empty($end))){
                $query->where('created_at', '>=', $start);
            }
            // 查询结束时间，没有开始时间
            if (empty($start) && !empty($end)){
                $query->where('created_at', '<=', $end);
            }
            // 查询开始，也查询结束
            if (!empty($start) && !empty($end)){
                $query->whereBetween('created_at', [$start, $end]);
            }
        })
            // 排序
            ->orderBy('id', 'desc')
            // 分页
            ->paginate(2)
            ->appends($request->all());
            //->get();
        return view('admin.index', ['admin' => $data]);
    }
    // 显示添加页面
    public function add(){
        return view('admin.admin_add');
    }
    // 添加管理员
    public function addPost(Request $request){
        $username = $request->input('username');
        $pass = $request->input('pass');
        $repass = $request->input('repass');
        // 验证是否为空
        if (!$username || !$pass || !$repass){
            return response()->json([
               'code' => 0,
               'msg' => '必填项不能为空',
               'data' => null,
            ]);
        }
        // 判断输入两次密码是否一致
        if ($pass != $repass){
            return response()->json([
               'code' => 0,
               'msg' => '二次密码不一致',
               'data' => null,
            ]);
        }
        // 检查用户名是否已经存在
        $model = new AdminModels();
        $admin = $model->where('username', '=', $username)->first();
        if (!empty($admin)){
            return response()->json([
                'code' => 0,
                'msg' => '用户名已经存在',
                'data' => null,
            ]);
        }
        // 入库
        $model->username = $username;
        $model->password = md5($pass);
        if ($model->save()){
            return response()->json([
                'code' => 1,
                'msg' => '添加成功',
                'data' => null,
            ]);
        }
        return response()->json([
            'code' => 0,
            'msg' => '添加失败',
            'data' => null,
        ]);
    }
    // 显示修改管理员页面
    public function  edit(Request $request){
        $id = $request->input('id');
        $model = new AdminModels();
        $admin = $model->where('id', '=', $id)->first();
        return view('admin.admin_edit', ['admin' => $admin]);
    }
    // 修改管理员
    public function editPost(Request $request){
        $username = $request->input('username');
        $pass = $request->input('pass');
        $repass = $request->input('repass');
        // 验证是否为空
        if (!$username || !$pass || !$repass){
            return response()->json([
                'code' => 0,
                'msg' => '必填项不能为空',
                'data' => null,
            ]);
        }
        // 判断输入两次密码是否一致
        if ($pass != $repass){
            return response()->json([
                'code' => 0,
                'msg' => '二次密码不一致',
                'data' => null,
            ]);
        }
        // 判断新旧密码是否一致
        $model = new AdminModels();
        $admin = $model->where('username', '=', $username)->first();
        if (md5($pass) == $admin->password){
            return response()->json([
               'code' => 0,
                'msg' => '新旧密码不能一致',
                'data' => null,
            ]);
        }
        // 入库
        $admin->username = $username;
        $admin->password = md5($pass);
        if ($admin->save()){
            return response()->json([
               'code' => 1,
               'msg' => '保存成功',
               'data' => null,
            ]);
        }
        return response()->json([
           'code' => 0,
           'msg' => '保存失败',
           'data' => null,
        ]);
    }
    // 删除管理员
    public function del(Request $request){
        $id = $request->input('id');
        if(AdminModels::destroy($id)){
            return response()->json([
               'code' => 1,
               'msg' => '删除成功',
               'data' => null,
            ]);
        }
        return response()->json([
           'code' => 0,
           'msg' => '删除失败',
           'data' => null,
        ]);
    }
}
